using System;
using System.IO;
using Microsoft.Build.Framework;

namespace FitNesse4Net.MSBuidTasks.Tasks
{
    public class RunSlimTestSuite : ITask
    {
        [Required] 
        public string WorkingDirectory { get; set; }

        [Required]
        public string TargetDirectory { get; set; }

        [Required]
        public string TestSuiteName { get; set; }

        #region ITask Members

        public bool Execute()
        {
            LogHelper.ConfigureLogging();

            ValidateInputParameters();

            var fitNesse = new FitNesse { WorkDirectoryPath = Path.GetFullPath(WorkingDirectory), TargetDirectory = Path.GetFullPath(TargetDirectory) };

            fitNesse.RunTestSuite(TestSuiteName);

            return true;
        }

        public IBuildEngine BuildEngine { get; set; }

        public ITaskHost HostObject { get; set; }

        #endregion

        private void ValidateInputParameters()
        {
            if (!Directory.Exists(WorkingDirectory))
            {
                throw new InvalidOperationException(string.Format("Can't find working directory: {0}", WorkingDirectory));
            }
        }
        
    }
}